$(document).ready(function() {
	// 默认选中第一位
	$('input[name=inlineRadioOptions]').get(0).checked = true;
	$(":radio").click(function() {
		var id = $(this).val();
		$.ajax({
			type : 'POST',
			url : ctx + '/product/findSkuRelById.do',
			data : {
				relId : id
			},
			dataType : 'json',
			timeout : 30000,
			context : $('body'),
			success : function(data) {
				if (data.code == "ok") {
					var obj = data.object;
					$("#stock").text(obj.stock);
					$("#price").text("¥" + obj.price.toFixed(2));
					$("#counts").text(1);
				} else {
					showTip2(data.msg);
				}
			},
			error : function(xhr, type) {
				showTip2('提交失败!');
			}
		});
	});

});

// 数量修改
function plus(flag) {
	var counts = $("#counts").text();
	var stock = $("#stock").text();
	var countsInt = parseInt(counts);
	var stockInt = parseInt(stock);
	if (flag == 1) {
		if (countsInt < stockInt) {
			countsInt = countsInt + 1;
		}
	}
	if (flag == 0) {
		if (countsInt > 1) {
			countsInt = countsInt - 1;
		}
	}
	$("#counts").text(countsInt);
}

/* 立即购买 */
function buyNow(productId) {
	var productSkuRelationId = $('input:radio:checked').val();
	var counts = $("#counts").text();
	window.location.href = decodeURIComponent(ctx + '/order/buyNow?productId='
			+ productId + "&productSkuRelId=" + productSkuRelationId + "&counts=" + counts);
}

/* 加入购物车 */
function addCart(id, name, smallImage) {
	var productSkuRelationId = $('input:radio:checked').val();
	var counts = $("#counts").text();
	$.ajax({
				type : 'POST',
				url : ctx + '/shop/addProductToCart.do',
				data : {
					productId : id,
					productName : name,
					smallImage : smallImage,
					productSkuRelId : productSkuRelationId,
					counts : counts
				},
				dataType : 'json',
				timeout : 30000,
				context : $('body'),
				success : function(data) {
					if (data.code == "ok") {
						var obj = data.object;
						//购物车显示编辑
						$("#edit").html('<a href="' + ctx + '/shop/shoppingCart">编辑</a>');
						// 购物车显示数量,总金额
						$("#cartTotalCounts").text(obj.counts);
						$("#cartTotalValue").text("¥" + obj.value.toFixed(2));

						// 购物车商品及数量显示
						var result = '';
						var items = obj.items;
						for (item in items) {
							item = items[item];
							result += '<div class="media">'
									+ '<a class="media-left media-middle" href="'+ctx+'/product/'
									+ item.productId
									+ '">'
									+ '<img class="avatar-md" src="'
									+ item.smallImage
									+ '" >'
									+ '</a>'
									+ '<div class="media-body media-middle">'
									+ '<a style="color: #333333" href="'+ctx+'/product/'
									+ item.productId + '" class="productName">'
									+ item.productName + '&nbsp'
									+ item.skuValue1 + item.skuValue2
									+ '&nbsp(' + item.counts + ')</a>'
									+ '</div>' + '</div>';

						}
						$("#cartItemList").html(result);
					} else {
						showTip2(data.msg);
					}
				},
				error : function(xhr, type) {
					showTip2('提交失败!');
				}
			});
}

function changeCollect(productId){
	var isCollect = $("#isCollect").val();
	var collect = $("#collect");
	$.ajax({
		type : 'POST',
		url : ctx + '/member/changCollectionStatus.do',
		data : {
			isCollection : isCollect,
			productId : productId,
			type : 1
		},
		dataType : 'json',
		timeout : 30000,
		context : $('body'),
		success : function(data) {
			if (data.code == "ok") {
				if(data.object == true){
					collect.html('<span class='+'"glyphicon glyphicon-star"'+'></span>取消收藏');
					$("#isCollect").val(true);
				}else{
					collect.html('<span class='+'"glyphicon glyphicon-star-empty"'+'></span>加入收藏');
					$("#isCollect").val(false);
				}
			} else {
				showTip2(data.msg);
			}
		},
		error : function(xhr, type) {
			showTip2('提交失败!');
		}
	});
}